<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="modelList">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="模型编号" prop="id" width="100" />
      <el-table-column
        label="模型名称"
        prop="name"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column
        label="模型路径"
        prop="path"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="模型材质" prop="mesh">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="`${rootPath}/${scope.row.mesh}`"
            fit="cover"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column label="类型" prop="type">
        <template slot-scope="scope">
          {{ scope.row.type | typeStr }}
        </template>
      </el-table-column>
      <el-table-column label="备注" prop="remark" />

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope" >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
           v-if="scope.row.type !== 'default'"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改模型配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="模型名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入模型名称" />
        </el-form-item>
        <el-form-item label="模型" prop="path">
          <el-input v-model="form.path" placeholder="请输入模型" readonly>
            <label slot="suffix">
              <el-upload
                action=""
                :http-request="(event) => uploadFile('path', event)"
                :show-file-list="false"
              >
                <el-button icon="el-icon-upload2"></el-button>
              </el-upload>
            </label>
          </el-input>
        </el-form-item>
        <el-form-item label="模型材质" prop="mesh">
          <el-input v-model="form.mesh" placeholder="请输入模型材质">
            <label slot="suffix">
              <el-upload
                action=""
                :http-request="(event) => uploadFile('mesh', event)"
                :show-file-list="false"
              >
                <el-button icon="el-icon-upload2"></el-button>
              </el-upload>
            </label>
          </el-input>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select
            v-model="form.type"
            placeholder="请选择模型类型"
            class="block"
          >
            <el-option
              v-for="item in modelTypes"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getModels,
  getModel,
  createModel,
  updateModel,
  removeModel,
} from "@/api/model";
import { uploadFile } from "@/api/file";

export default {
  name: "Model",
  data() {
    return {
      rootPath: process.env.VUE_APP_ROOT_PATH,
      // 遮罩层
      loading: true,
      // 模型表格数据
      modelList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "模型名称不能为空", trigger: "blur" },
        ],
        path: [{ required: true, message: "请上传模型", trigger: "blur" }],

        type: [{ required: true, message: "请选择类型", trigger: "blur" }],
      },
      modelTypes: [
        {
          label: "默认",
          value: "default",
        },
        {
          label: "风格",
          value: "style",
        },
      ],
    };
  },
  filters: {
    typeStr(value) {
      if (!value) return "";
      const { label } = [
        {
          label: "默认",
          value: "default",
        },
        {
          label: "风格",
          value: "style",
        },
      ].find((item) => item.value == value);
      return label || "";
    },
  },
  created() {
    this.getModels();
  },
  methods: {
    async uploadFile(target, event) {
      const formData = new FormData();
      formData.append("file", event.file);

      const { result } = await uploadFile(formData);
      this.form = {
        ...this.form,
        [target]: result,
      };
    },
    /** 查询模型列表 */
    getModels() {
      this.loading = true;
      getModels().then(({ result }) => {
        this.modelList = result;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        path: undefined,
        mesh: undefined,
        remark: undefined,
        type: undefined,
      };
      this.resetForm("form");
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加模型";
    },
    /** 修改按钮操作 */
    handleUpdate({ id }) {
      getModel(id).then(({ result }) => {
        this.form = result;
        this.open = true;
        this.title = "修改模型";
      });
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            const { id, ...model } = this.form;
            updateModel(id, model).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getModels();
            });
          } else {
            createModel(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getModels();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal
        .confirm('是否确认删除模型编号为"' + id + '"的数据项？')
        .then(function () {
          return removeModel(id);
        })
        .then(() => {
          this.getModels();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
